import 'package:flutter/material.dart';

/// 自定义动画；不要被名字给误导了
/// 动画有变化就构建小部件
class AnimMyLoveWiget extends StatefulWidget {
  const AnimMyLoveWiget({Key? key}) : super(key: key);

  @override
  _AnimMyLoveWigetState createState() => _AnimMyLoveWigetState();
}

class _AnimMyLoveWigetState extends State<AnimMyLoveWiget>
    with TickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation sizeAnim;
  late Animation colorAnim;

  // 控制动画曲线
  late CurvedAnimation curveAnim;

  @override
  void initState() {
    super.initState();

    _animationController =
        AnimationController(vsync: this, duration: Duration(seconds: 1));
    _animationController.addListener(() {
      setState(() {});
    });

    // 未使用动画曲线
    // sizeAnim = Tween(begin: 32.0,end: 150.0).animate(_animationController);
    // colorAnim = ColorTween(begin: Colors.red,end: Colors.blue).animate(_animationController);

    // 使用动画曲线
    curveAnim =
        CurvedAnimation(parent: _animationController, curve: Curves.easeInOut);
    sizeAnim = Tween(begin: 32.0, end: 150.0).animate(curveAnim);
    colorAnim =
        ColorTween(begin: Colors.red, end: Colors.blue).animate(curveAnim);
  }

  @override
  void dispose() {
    super.dispose();
    _animationController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimHeart(
      animlist: [sizeAnim, colorAnim],
      controller: _animationController,
    );
  }
}

class AnimHeart extends AnimatedWidget {
  final List animlist;
  final AnimationController controller;

  AnimHeart({required this.animlist, required this.controller})
      : super(listenable: controller);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: IconButton(
          onPressed: () {
            switch (controller.status) {
              case AnimationStatus.completed:
                // 反转执行动画
                controller.reverse();
                break;
              default:
                // 执行动画
                controller.forward();
                break;
            }
          },
          iconSize: animlist[0].value,
          color: animlist[1].value,
          icon: Icon(Icons.star),
        ),
      ),
    );
  }
}
